package com.example.demo.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.demo.VO.MeetingDetailVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.demo.common.R;
import com.example.demo.entity.JoinMeeting;
import com.example.demo.entity.Meeting;
import com.example.demo.DTO.NewMeetingDTO;
import com.example.demo.entity.User;
import com.example.demo.entity.*;
import com.example.demo.service.JoinMeetingService;
import com.example.demo.service.MeetingService;
import com.example.demo.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/Meeting")
public class MeetingController {
    @Autowired
    private MeetingService meetingService;
    @Autowired
    private JoinMeetingService joinMeetingService;

    /**
     * 查找每个user对应的会议
     *
     * @param user user类
     * @return 返回查找到的meeting集合
     */
    @PostMapping("/getList")
    public R<List<Meeting>> getMeetingList(@RequestBody User user) {
        List<String> ret = getEqMeeting(user);
        if(ret.isEmpty()){
            return R.success(null);
        }
        List<Meeting> res = meetingService.getByMeetingId(ret,user.getUserId());

        return R.success(res);
    }
    @PostMapping("/getHostList")
    public R<List<Meeting>> getHostList (@RequestBody User user) {
        List<String> ret = getEqMeeting(user);
        if(ret.isEmpty()){
            return R.success(null);
        }
        List<Meeting> res = meetingService.getMeetingByHost(ret, user.getUserId());
        return R.success(res);
    }
    @PostMapping("/getJoinList")
    public R<List<Meeting>> getJoinList (@RequestBody User user) {

        List<String> ret = getEqMeeting(user);
        if(ret.isEmpty()){
            return R.success(null);
        }

        LambdaQueryWrapper<Meeting> queryWrapper = new LambdaQueryWrapper<Meeting>()
                .in(Meeting::getMeetingId,ret);
        List<Meeting> res = meetingService.list(queryWrapper);
        return R.success(res);


    }
    /**
     * 查找所有会议
     * @return 返回查找到的meeting集合
     */
    @GetMapping("/getAllList")
    public R<List<Meeting>> getAllMeeting() {
        List<Meeting> list = meetingService.list();
        return R.success(list);
    }
    /**
     * 查找参加某个会议的参会人员
     * @return 返回查找到的Joinmeeting集合
     */
    @PostMapping("/getPartList")
    public R<List<JoinMeeting>> getAllPart(@RequestBody Meeting meeting) {
        QueryWrapper<JoinMeeting> queryWrapper = new QueryWrapper<>();
        String meetingId = meeting.getMeetingId();
        queryWrapper.eq("meeting_id",meetingId );
        List<JoinMeeting> list = joinMeetingService.list(queryWrapper);
        return R.success(list);
    }
    /**
     * 查找参加某个会议的参会人员的ID的list集合
     * @return 返回查找到的string集合
     */
    @PostMapping("/getPartListOnlyUserId")
    public R<List<String>> getAllPartOnlyUserId(@RequestBody Meeting meeting) {
        QueryWrapper<JoinMeeting> queryWrapper = new QueryWrapper<>();
        String meetingId = meeting.getMeetingId();
        System.out.print(meetingId);
        queryWrapper.eq("meeting_id",meetingId ).select("user_id");
        List<JoinMeeting> list = joinMeetingService.list(queryWrapper);
        List<String> res =list.stream().map(JoinMeeting::getUserId).collect(Collectors.toList());
        return R.success(res);
    }

    @PutMapping("/UpdateMeeting")
    public R<String> UpdateMeeting(@RequestBody NewMeetingDTO newMeetingDTO) {
        Meeting meeting = new Meeting();
        BeanUtils.copyProperties(newMeetingDTO, meeting);
        UpdateWrapper<Meeting> updateWrapper = new UpdateWrapper<>();
        UpdateWrapper<JoinMeeting> updatePartWrapper = new UpdateWrapper<>();

        String meetingId = meeting.getMeetingId();

        updateWrapper.eq("meeting_id",meetingId);
        updatePartWrapper.eq("meeting_id",meetingId);
        meetingService.update(meeting,updateWrapper);
        List<String> meetingPeople = newMeetingDTO.getMeetingPeople();

        List<JoinMeeting> joinMeetings = meetingPeople.stream().map(eachOne -> {
            JoinMeeting joinMeeting = new JoinMeeting();
            joinMeeting.setUserId(eachOne);
            joinMeeting.setMeetingId(meetingId);
            return joinMeeting;
        }).collect(Collectors.toList());
        QueryWrapper<JoinMeeting> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("meeting_id",meetingId );
        joinMeetingService.remove(queryWrapper);
        //joinMeetingService.saveOrUpdateBatch(joinMeetings);
        //joinMeetingService.updateBatchById(joinMeetings);

        joinMeetingService.saveBatch(joinMeetings);
        return R.success("修改会议成功");
    }

    @PutMapping("/DeleteMeeting")
    public R<String> DeleteMeeting(@RequestBody Meeting meeting) {
        QueryWrapper<JoinMeeting> queryWrapper = new QueryWrapper<>();
        QueryWrapper<Meeting> wrapper = new QueryWrapper<>();
        String meetingId = meeting.getMeetingId();
        wrapper.eq("meeting_id",meetingId);
        meetingService.remove(wrapper);
        queryWrapper.eq("meeting_id",meetingId );
        joinMeetingService.remove(queryWrapper);
        return R.success("删除会议成功");
    }
    /**
     * 查找当日会议
     *
     * @param user user类
     * @return 返回查找到的meeting集合
     */
    @PostMapping("/getRecentList")
    public R<List<Meeting>> getRecentMeeting(@RequestBody User user) {
        List<String> ret = getEqMeeting(user);
        if(ret.isEmpty()){
            return R.success(null);
        }
        List<Meeting> res = meetingService.getRencentByMeetingDate(ret);
        return R.success(res);
    }

    /**
     * 新建会议
     * @param newMeetingDTO 来自前端的会议数据
     * @return 返回
     */
    @Transactional
    @PostMapping("/newCon")
    public R<String> newConference(@RequestBody NewMeetingDTO newMeetingDTO) {
        Meeting meeting = new Meeting();
        BeanUtils.copyProperties(newMeetingDTO, meeting);
        meetingService.save(meeting);
        List<String> meetingPeople = newMeetingDTO.getMeetingPeople();
        String meetingId = meeting.getMeetingId();
        List<JoinMeeting> joinMeetings = meetingPeople.stream().map(eachOne -> {
            JoinMeeting joinMeeting = new JoinMeeting();
            joinMeeting.setUserId(eachOne);
            joinMeeting.setMeetingId(meetingId);
            return joinMeeting;
        }).collect(Collectors.toList());
        joinMeetingService.saveBatch(joinMeetings);
        return R.success("新建会议成功");

    }

    /**
     * 获取会议的详细信息
     * @param meeting 传query进来
     * @return 返回MeetingDTO
     */
    @PostMapping("/getMeetingDetails")
    public R<MeetingDetailVO> meetingDetail(@RequestBody Meeting meeting){
        MeetingDetailVO meetingDetailVO = new MeetingDetailVO();
        LambdaQueryWrapper<Meeting> queryWrapper = new LambdaQueryWrapper<Meeting>()
                .eq(Meeting::getMeetingId,meeting.getMeetingId());
        BeanUtils.copyProperties(meetingService.getOne(queryWrapper),meetingDetailVO);
        meetingDetailVO.setMeetingPeople(joinMeetingService.getMeetingPeopleByMeetingId(meeting.getMeetingId()));
        return R.success(meetingDetailVO);
    }

    @PostMapping("/cancelMeeting")
    public R<String> cancelMeeting(@RequestBody Meeting meeting){
        LambdaQueryWrapper<JoinMeeting> queryWrapper = new LambdaQueryWrapper<JoinMeeting>()
                .eq(JoinMeeting::getMeetingId,meeting.getMeetingId());
        joinMeetingService.remove(queryWrapper);
        meetingService.removeById(meeting.getMeetingId());
        return R.success("取消成功");
    }

    public List<String> getEqMeeting(User user) {
        LambdaQueryWrapper<JoinMeeting> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(JoinMeeting::getUserId, user.getUserId());
        List<JoinMeeting> list = joinMeetingService.list(lambdaQueryWrapper);
        return list.stream().map(JoinMeeting::getMeetingId).collect(Collectors.toList());
    }
}
